﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OP20.ViewModels
{
    public class PressureCheckViewModel : BaseViewModel
    {
        public PressureCheckViewModel(IRegionManager regionManager) : base(regionManager)
        {
            InitParam();

            var QG1 = new CylinderModel("ST5测高上下气缸", 0, false, false);
            var QG2 = new CylinderModel("ST10上下气缸", 0, false, false);
            var QG3 = new CylinderModel("ST13压合气缸", 0, false, false);


            ST5QGList.Add(QG1);
            ST10QGList.Add(QG2);
            ST13QGList.Add(QG3);
            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();
        }

        private void OnTick(object? sender, EventArgs e)
        {
            ST5CurrentValue = OtherDevicePLCToPC_Global.testData2s[0].CurrentValue;
            ST10CurrentValue = OtherDevicePLCToPC_Global.testData2s[1].CurrentValue;
            ST13CurrentValue = OtherDevicePLCToPC_Global.testData2s[2].CurrentValue;

            foreach (var item in ST5QGList)
            {

                switch (item.Name)
                {
                    case "ST5测高上下气缸":
                        item.QG_Status = ST05UpDownCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST05UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST05UpDownCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                }
            }

            foreach (var item in ST10QGList)
            {

                switch (item.Name)
                {
                    case "ST10上下气缸":
                        item.QG_Status = ST10UpDownCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST10UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST10UpDownCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                }
            }

            foreach (var item in ST13QGList)
            {

                switch (item.Name)
                {
                    case "ST13压合气缸":
                        item.QG_Status = ST13PressInCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST13PressInCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST13PressInCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                }
            }
        }

        private void InitParam()
        {
            ST5CalibrationValue = TestPressure_Global.testPressures[0].CurrentValue;
            ST5ErrorRange = TestPressure_Global.testPressures[0].ErrorRange;

            ST10CalibrationValue = TestPressure_Global.testPressures[1].CurrentValue;
            ST10ErrorRange = TestPressure_Global.testPressures[1].ErrorRange;

            ST13CalibrationValue = TestPressure_Global.testPressures[2].CurrentValue;
            ST13ErrorRange = TestPressure_Global.testPressures[2].ErrorRange;
        }


        #region 事件

        private DelegateCommand<string> _QGDownCommand;
        public DelegateCommand<string> QGDownCommand =>
            _QGDownCommand ?? (_QGDownCommand = new DelegateCommand<string>(ExecuteQGDownCommand));

        void ExecuteQGDownCommand(string parameter)
        {
            switch (parameter)
            {

                case "ST5测高上下气缸":
                    ST05UpDownCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;
                case "ST10上下气缸":
                    ST10UpDownCy.cylinderPCToPLC_Global.bManualBT = true;

                    break;
                case "ST13压合气缸":
                    ST13PressInCy.cylinderPCToPLC_Global.bManualBT = true;

                    break;
                default:
                    break;
            }
        }


        private DelegateCommand<string> _QGUpCommand;
        public DelegateCommand<string> QGUpCommand =>
            _QGUpCommand ?? (_QGUpCommand = new DelegateCommand<string>(ExecuteQGUpCommand));

        void ExecuteQGUpCommand(string parameter)
        {
            switch (parameter)
            {

                case "ST5测高上下气缸":
                    ST05UpDownCy.cylinderPCToPLC_Global.bManualBT = false;
                    break;
                case "ST10上下气缸":
                    ST10UpDownCy.cylinderPCToPLC_Global.bManualBT = false;

                    break;
                case "ST13压合气缸":
                    ST13PressInCy.cylinderPCToPLC_Global.bManualBT = false;

                    break;
                default:
                    break;
            }
        }


        private DelegateCommand _ST5Reset0DownCmd;
        public DelegateCommand ST5Reset0DownCmd =>
            _ST5Reset0DownCmd ?? (_ST5Reset0DownCmd = new DelegateCommand(ExecuteST5Reset0DownCmd));

        void ExecuteST5Reset0DownCmd()
        {
            OtherDevicePCToPLC_Global.ST5PressBoolArr[0] = true;
        }

        private DelegateCommand _ST5Reset0UpCmd;
        public DelegateCommand ST5Reset0UpCmd =>
            _ST5Reset0UpCmd ?? (_ST5Reset0UpCmd = new DelegateCommand(ExecuteST5Reset0UpCmd));

        void ExecuteST5Reset0UpCmd()
        {
            OtherDevicePCToPLC_Global.ST5PressBoolArr[0] = false;

        }

        private DelegateCommand _ST5ModeDownCmd;
        public DelegateCommand ST5ModeDownCmd =>
            _ST5ModeDownCmd ?? (_ST5ModeDownCmd = new DelegateCommand(ExecuteST5ModeDownCmd));

        void ExecuteST5ModeDownCmd()
        {
            OtherDevicePCToPLC_Global.ST5PressBoolArr[1] = true;

        }

        private DelegateCommand _ST5ModeUpCmd;
        public DelegateCommand ST5ModeUpCmd =>
            _ST5ModeUpCmd ?? (_ST5ModeUpCmd = new DelegateCommand(ExecuteST5ModeUpCmd));

        void ExecuteST5ModeUpCmd()
        {
            OtherDevicePCToPLC_Global.ST5PressBoolArr[1] = false;

        }

        private DelegateCommand _ST5TestDownCmd;
        public DelegateCommand ST5TestDownCmd =>
            _ST5TestDownCmd ?? (_ST5TestDownCmd = new DelegateCommand(ExecuteST5TestDownCmd));

        void ExecuteST5TestDownCmd()
        {
            OtherDevicePCToPLC_Global.ST5PressBoolArr[2] = true;

        }

        private DelegateCommand _ST5TestUpCmd;
        public DelegateCommand ST5TestUpCmd =>
            _ST5TestUpCmd ?? (_ST5TestUpCmd = new DelegateCommand(ExecuteST5TestUpCmd));

        void ExecuteST5TestUpCmd()
        {
            OtherDevicePCToPLC_Global.ST5PressBoolArr[2] = false;

        }


        private DelegateCommand _ST10Reset0DownCmd;
        public DelegateCommand ST10Reset0DownCmd =>
            _ST10Reset0DownCmd ?? (_ST10Reset0DownCmd = new DelegateCommand(ExecuteST10Reset0DownCmd));

        void ExecuteST10Reset0DownCmd()
        {
            OtherDevicePCToPLC_Global.ST10PressBoolArr[0] = true;

        }

        private DelegateCommand _ST10Reset0UpCmd;
        public DelegateCommand ST10Reset0UpCmd =>
            _ST10Reset0UpCmd ?? (_ST10Reset0UpCmd = new DelegateCommand(ExecuteST10Reset0UpCmd));

        void ExecuteST10Reset0UpCmd()
        {
            OtherDevicePCToPLC_Global.ST10PressBoolArr[0] = false;

        }

        private DelegateCommand _ST10ModeDownCmd;
        public DelegateCommand ST10ModeDownCmd =>
            _ST10ModeDownCmd ?? (_ST10ModeDownCmd = new DelegateCommand(ExecuteST10ModeDownCmd));

        void ExecuteST10ModeDownCmd()
        {
            OtherDevicePCToPLC_Global.ST10PressBoolArr[1] = true;

        }

        private DelegateCommand _ST10ModeUpCmd;
        public DelegateCommand ST10ModeUpCmd =>
            _ST10ModeUpCmd ?? (_ST10ModeUpCmd = new DelegateCommand(ExecuteST10ModeUpCmd));

        void ExecuteST10ModeUpCmd()
        {
            OtherDevicePCToPLC_Global.ST10PressBoolArr[1] = false;

        }

        private DelegateCommand _ST10TestDownCmd;
        public DelegateCommand ST10TestDownCmd =>
            _ST10TestDownCmd ?? (_ST10TestDownCmd = new DelegateCommand(ExecuteST10TestDownCmd));

        void ExecuteST10TestDownCmd()
        {
            OtherDevicePCToPLC_Global.ST10PressBoolArr[2] = true;

        }

        private DelegateCommand _ST10TestUpCmd;
        public DelegateCommand ST10TestUpCmd =>
            _ST10TestUpCmd ?? (_ST10TestUpCmd = new DelegateCommand(ExecuteST10TestUpCmd));

        void ExecuteST10TestUpCmd()
        {
            OtherDevicePCToPLC_Global.ST10PressBoolArr[2] = false;

        }


        private DelegateCommand _ST13Reset0DownCmd;
        public DelegateCommand ST13Reset0DownCmd =>
            _ST13Reset0DownCmd ?? (_ST13Reset0DownCmd = new DelegateCommand(ExecuteST13Reset0DownCmd));

        void ExecuteST13Reset0DownCmd()
        {
            OtherDevicePCToPLC_Global.ST13PressBoolArr[0] = true;

        }

        private DelegateCommand _ST13Reset0UpCmd;
        public DelegateCommand ST13Reset0UpCmd =>
            _ST13Reset0UpCmd ?? (_ST13Reset0UpCmd = new DelegateCommand(ExecuteST13Reset0UpCmd));

        void ExecuteST13Reset0UpCmd()
        {

            OtherDevicePCToPLC_Global.ST13PressBoolArr[0] = false;

        }

        private DelegateCommand _ST13ModeDownCmd;
        public DelegateCommand ST13ModeDownCmd =>
            _ST13ModeDownCmd ?? (_ST13ModeDownCmd = new DelegateCommand(ExecuteST13ModeDownCmd));

        void ExecuteST13ModeDownCmd()
        {
            OtherDevicePCToPLC_Global.ST13PressBoolArr[1] = true;

        }

        private DelegateCommand _ST13ModeUpCmd;
        public DelegateCommand ST13ModeUpCmd =>
            _ST13ModeUpCmd ?? (_ST13ModeUpCmd = new DelegateCommand(ExecuteST13ModeUpCmd));

        void ExecuteST13ModeUpCmd()
        {
            OtherDevicePCToPLC_Global.ST13PressBoolArr[1] = false;

        }

        private DelegateCommand _ST13TestDownCmd;
        public DelegateCommand ST13TestDownCmd =>
            _ST13TestDownCmd ?? (_ST13TestDownCmd = new DelegateCommand(ExecuteST13TestDownCmd));

        void ExecuteST13TestDownCmd()
        {
            OtherDevicePCToPLC_Global.ST13PressBoolArr[2] = true;

        }

        private DelegateCommand _ST13TestUpCmd;
        public DelegateCommand ST13TestUpCmd =>
            _ST13TestUpCmd ?? (_ST13TestUpCmd = new DelegateCommand(ExecuteST13TestUpCmd));

        void ExecuteST13TestUpCmd()
        {
            OtherDevicePCToPLC_Global.ST13PressBoolArr[2] = false;

        }


        private DelegateCommand<string> _TextChangeCommand;
        public DelegateCommand<string> TextChangeCommand =>
            _TextChangeCommand ?? (_TextChangeCommand = new DelegateCommand<string>(ExecuteTextChangeCommand));

        void ExecuteTextChangeCommand(string p)
        {

            switch (p)
            {
                case "ST5CalibrationValue":

                    TestPressure_Global.testPressures[0].CurrentValue = ST5CalibrationValue;
                    break;
                case "ST5ErrorRange":
                    TestPressure_Global.testPressures[0].ErrorRange = ST5ErrorRange;

                    break;

                case "ST10CalibrationValue":
                    TestPressure_Global.testPressures[1].CurrentValue = ST10CalibrationValue;


                    break;
                case "ST10ErrorRange":
                    TestPressure_Global.testPressures[1].ErrorRange = ST10ErrorRange;
                    break;

                case "ST13CalibrationValue":
                    TestPressure_Global.testPressures[2].CurrentValue = ST13CalibrationValue;

                    break;
                case "ST13ErrorRange":
                    TestPressure_Global.testPressures[2].ErrorRange = ST13ErrorRange;

                    break;
                default:
                    break;
            }
        }
        #endregion


        #region 属性
        private ObservableCollection<CylinderModel> _ST5QGList;
        public ObservableCollection<CylinderModel> ST5QGList
        {
            get { return _ST5QGList ?? (_ST5QGList = new ObservableCollection<CylinderModel>()); }
            set { SetProperty(ref _ST5QGList, value); }
        }

        private ObservableCollection<CylinderModel> _ST10QGList;
        public ObservableCollection<CylinderModel> ST10QGList
        {
            get { return _ST10QGList ?? (_ST10QGList = new ObservableCollection<CylinderModel>()); }
            set { SetProperty(ref _ST10QGList, value); }
        }

        private ObservableCollection<CylinderModel> _ST13QGList;
        public ObservableCollection<CylinderModel> ST13QGList
        {
            get { return _ST13QGList ?? (_ST13QGList = new ObservableCollection<CylinderModel>()); }
            set { SetProperty(ref _ST13QGList, value); }
        }


        private float _ST5CalibrationValue;
        public float ST5CalibrationValue
        {
            get { return _ST5CalibrationValue; }
            set { SetProperty(ref _ST5CalibrationValue, value); }
        }

        private float _ST5ErrorRange;
        public float ST5ErrorRange
        {
            get { return _ST5ErrorRange; }
            set { SetProperty(ref _ST5ErrorRange, value); }
        }

        private float _ST5CurrentValue;
        public float ST5CurrentValue
        {
            get { return _ST5CurrentValue; }
            set { SetProperty(ref _ST5CurrentValue, value); }
        }

        private float _ST10CalibrationValue;
        public float ST10CalibrationValue
        {
            get { return _ST10CalibrationValue; }
            set { SetProperty(ref _ST10CalibrationValue, value); }
        }

        private float _ST10ErrorRange;
        public float ST10ErrorRange
        {
            get { return _ST10ErrorRange; }
            set { SetProperty(ref _ST10ErrorRange, value); }
        }

        private float _ST10CurrentValue;
        public float ST10CurrentValue
        {
            get { return _ST10CurrentValue; }
            set { SetProperty(ref _ST10CurrentValue, value); }
        }

        private float _ST13CalibrationValue;
        public float ST13CalibrationValue
        {
            get { return _ST13CalibrationValue; }
            set { SetProperty(ref _ST13CalibrationValue, value); }
        }

        private float _ST13ErrorRange;
        public float ST13ErrorRange
        {
            get { return _ST13ErrorRange; }
            set { SetProperty(ref _ST13ErrorRange, value); }
        }

        private float _ST13CurrentValue;
        public float ST13CurrentValue
        {
            get { return _ST13CurrentValue; }
            set { SetProperty(ref _ST13CurrentValue, value); }
        }
        #endregion
    }
}
